package com.syh.jn.scm.service.impl.supplier.finance;

import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.supplier.entity.finance.ReceiptBill;
import com.syh.jn.scm.domain.supplier.event.finance.ReceiptBillEvent;
import com.syh.jn.scm.domain.supplier.repository.finance.ReceiptBillRepository;
import com.syh.jn.scm.dto.supplier.finance.ReceiptBillMto;
import com.syh.jn.scm.dto.supplier.finance.ReceiptBillSto;
import com.syh.jn.scm.dto.supplier.finance.ReceiptBillVo;
import com.syh.jn.scm.service.api.supplier.finance.ReceiptBillService;
import com.syh.jn.scm.service.api.supplier.finance.enums.ReceiptBillEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 收款单接口实现
 */
@Service
@Transactional
public class ReceiptBillServiceImpl extends AbstractServiceImpl<ReceiptBillSto, ReceiptBillMto, ReceiptBillVo, ReceiptBill, Long> implements ReceiptBillService {

  @Autowired
  private ReceiptBillRepository receiptBillRepositoryImpl;

  @Override
  protected AggregateRepository<ReceiptBill, Long> getAggregateRepository() {
    return receiptBillRepositoryImpl;
  }

  /**
   * 保存收款单
   *
   * @param sto
   * @return
   */
  @Override
  public Long save(ReceiptBillSto sto) {
    ReceiptBill bill = new ReceiptBill(sto);
    Long id = receiptBillRepositoryImpl.add(bill);
    this.publicEvent(ReceiptBillEvent.receiptBillCreated(new Object[]{bill}));
    return id;
  }

  /**
   * 修改付款单
   *
   * @param mto
   */
  @Override
  public void update(ReceiptBillMto mto) {
    ReceiptBill bill = receiptBillRepositoryImpl.getById(mto.getId());
    bill.modify(mto);
    receiptBillRepositoryImpl.update(bill);
  }

  /**
   * 删除付款单
   *
   * @param id
   */
  @Override
  public void delete(Long id) {
    ReceiptBill bill = receiptBillRepositoryImpl.getById(id);
    bill.destroy();
    receiptBillRepositoryImpl.remove(bill);
    this.publicEvent(ReceiptBillEvent.receiptBillDestory(new Object[]{bill}));
  }

  /**
   * 财务确认到账，更新对账单金额
   *
   * @param id
   */
  @Override
  public void confirm(Long id) {
    ReceiptBill bill = receiptBillRepositoryImpl.getById(id);
    bill.confirm();
    receiptBillRepositoryImpl.update(bill);

    //设置对账单的到账金额
    //发布事件消息
    if (bill.getLockStatus().equals(ReceiptBillEnum.LOCK.getStatus())) {
      this.publicEvent(ReceiptBillEvent.receiptBillLock(new Object[]{bill}));
    }
  }

  /**
   * 取消到账
   *
   * @param id
   */
  @Override
  public void cancel(Long id) {
    ReceiptBill bill = receiptBillRepositoryImpl.getById(id);
    bill.cancel();
    receiptBillRepositoryImpl.update(bill);

    //设置对账单的到账金额为0,状态为未收款或者部分收款
    //发布事件消息(不锁定状态和下游是已确认才会发送事件)
    if (bill.getLockStatus().equals(ReceiptBillEnum.UNLOCK.getStatus()) && bill.getConfirmState().equals(ReceiptBillEnum.OTHERPARTY_CONFIRM.getStatus())) {
      this.publicEvent(ReceiptBillEvent.receiptBillUnlock(new Object[]{bill}));
    }
  }

  /**
   * 解锁
   *
   * @param id
   */
  @Override
  public void unLock(Long id) {
    ReceiptBill bill = receiptBillRepositoryImpl.getById(id);
    bill.unLock();
    receiptBillRepositoryImpl.update(bill);
  }
}
